package com.lanou.activity.controller;

import com.lanou.activity.Service.PlainSupService;
import com.lanou.activity.utils.ResultUtil;
import com.lanou.activity.utils.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/plsup")
public class PlainSupRest {

    private final Logger logger = LoggerFactory.getLogger(plainInfoRest.class);

    @Resource
    private PlainSupService plainSupService;


    /**
     * @param params prdNo商品编号(prdNos)、vipLevel会员等级（int）、total总价（double）、积分record（double）
     * @return 赠品名prd_dtl_name（list ）、总价price（double）、总积分record（double）
     * @description 计算参加企划应该给予的优惠
     * @author Lonely
     * @date 17/9/27
     */
    @PostMapping("getPlainSupInfo")
    @ResponseBody
    public Map<String, Object> getPlainSupInfo(@RequestBody Map<String, Object> params) {
        System.out.println("传递的数据 = " + params);

//        获取所有商品编号
        List aaa = (List) params.get("prdNos");
//        获取总价
        String total1 = (String) params.get("total");
//        获取会员等级vipLevel
        String vipLevel1 = (String) params.get("vipLevel");
//        获取积分
        String record1 = (String) params.get("record");



//        根据prdNo连表查询（plain_info、sale_cdn_info、plan_prd_r）
        List<Map<String, Object>> plainInfoAll = plainSupService.findPlainInfoAll(aaa);
        System.out.println("plainInfoAll = " + plainInfoAll);

//        遍历获取conNo集合
        List conNos = new ArrayList();
        for (int i = 0;i<plainInfoAll.size();i++) {
//            conNo
            String conNo = (String) plainInfoAll.get(i).get("con_no");

            conNos.add(conNo);
            System.out.println("conNos = " + conNos);
        }
//        根据prdNo连表查询信息
        List<Map<String,Object>> saleCdnInfo = plainSupService.findSaleCdnByConNo(conNos);

//      获取sale_cdn_info表的限定条件值并判断取最大值
        int value = 0;
        int vip = 0;
        for (int i = 0 ; i<saleCdnInfo.size(); i++) {
//            限定条件
            String conValue = (String) saleCdnInfo.get(i).get("con_value");
            int conValue1 = Integer.parseInt(conValue);
            if (conValue1 >= value) {
                value = conValue1;
            }else {
                value = value;
            }
            System.out.println("value = " + value) ;

//			会员等级判断
            int conVipLevel = (int) saleCdnInfo.get(i).get("con_vip_level");
            if (conVipLevel <= vip) {
                vip = conVipLevel;
            } else {
                vip = vip;
            }
            System.out.println("vip = " + vip);
        }

//        类型转换
        double total = Double.parseDouble(total1);
        int vipLevel = Integer.parseInt(vipLevel1);
        double record = Double.parseDouble(record1);

//        判断总价是否满足限定值
        if (total >= value) {
//            判断会员等级是否满足促销条件的会员等级
            if (vipLevel>=vip) {

//				计算的优惠值
                int discount = 0;
//				赠送的商品名
                List prdDtlNames = new ArrayList();

                for (int i = 0;i<saleCdnInfo.size(); i++) {
//					获取优惠类型
                    String conType = (String) saleCdnInfo.get(i).get("con_type");
                    String conNo = (String) saleCdnInfo.get(i).get("con_no");

                    if (conType.equals("满赠")) {
                        String prd_dtl_name = null;
                        Map<String, Object> map = new HashMap<>();
                        List<Map<String, Object>> slgfInfo = plainSupService.findSLGFByConNo(conNo);
                        System.out.println("slgfInfo = " + slgfInfo);
                        for (int name = 0 ; name < slgfInfo.size();name++) {
                            String prd_dtl_id = (String) slgfInfo.get(name).get("g_prd_no");
                            map.put("prd_dtl_id",prd_dtl_id);
//                            调用商品明细接口 根据 商品分类名称 prdName 查询 商品明细编号prd_dtl_id和名字prd_dtl_name
                            List<Map<String, Object>> prdInfo = (List<Map<String, Object>>) ToolUtil.callUrl("http://10.90.86.249:8888/prd/getAll2", map);
//                    	      获取赠送的商品名
                            for (int dtl = 0;dtl<prdInfo.size();dtl++) {
                                prd_dtl_name = (String) prdInfo.get(dtl).get("prd_dtl_name");
                                prdDtlNames.add(prd_dtl_name);
                            }
                            System.out.println("prdDtlNames = " + prdDtlNames);
                        }

                    } else {
                        if (conType.equals("折扣")) {
                            List<Map<String, Object>> discountInfo = plainSupService.findDiscountByConNo(conNo,conType);
                            System.out.println("折扣discountInfo = " + discountInfo);
                            System.out.println("length = " + discountInfo.size());
                            for (int j = 0; j < discountInfo.size(); j++) {
                                int disValue = (int) discountInfo.get(j).get("dis_value");
                                total = total * (disValue / 10.0);
                            }
                            System.out.println("折扣过的total = " + total);

                        } else{

                            if (conType.equals("积分")) {
                                List<Map<String, Object>> discountInfo = plainSupService.findDiscountByConNo(conNo,conType);
                                System.out.println("积分discountInfo = " + discountInfo);
                                System.out.println("length = " + discountInfo.size());
                                for (int j = 0; j < discountInfo.size(); j++) {
                                    int disValue = (int) discountInfo.get(j).get("dis_value");
                                    record += ((total / 100.0) * disValue);
                                }
                                System.out.println("积分record = " + record);

                            } else {
                                List<Map<String, Object>> discountInfo = plainSupService.findDiscountByConNo(conNo,conType);
                                System.out.println("discountInfo = " + discountInfo);
                                System.out.println("length = " + discountInfo.size());
                                for (int j = 0;j < discountInfo.size();j++) {
                                    int disValue = (int) discountInfo.get(j).get("dis_value");
                                    discount += disValue;
                                    System.out.println("discountfor = " + discount);
                                }
                                System.out.println("discount = " + discount);
                            }
                        }

                    }

                }

//				  出参
                Map<String,Object> param = new HashMap<>();
//				返回的优惠过的价钱
                double price = total - discount;
                param.put("price",price);
                param.put("prdDtlName",prdDtlNames);
                param.put("record",record);
                System.out.println("出参param = " + param);

                return ResultUtil.success(param);

            } else {
                List listResult = new ArrayList();
                Map mapAll = new HashMap();
                mapAll.put("result", "会员等级不够");
                listResult.add(0, mapAll);
                return ResultUtil.success(listResult);
            }
        } else {
            List listResult = new ArrayList();
            Map mapAll = new HashMap();
            mapAll.put("result", "不满足优惠条件");
            listResult.add(0, mapAll);
            return ResultUtil.success(listResult);
        }

    }

}



